package health.lm.com.shop.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.jakewharton.rxbinding2.view.RxView;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.vondear.rxtool.view.RxToast;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.component_base.widget.SuperDividerItemDecoration;
import health.lm.com.router.ARouterConstant;
import health.lm.com.shop.R;
import health.lm.com.shop.R2;
import health.lm.com.shop.adapter.HLDynamicDetailCommentAdapter;
import health.lm.com.shop.entity.HLDynamicDetailEntity;
import health.lm.com.shop.presenter.HLDynamicDetailPresenter;

/**
 * Introduce:
 * <p>
 * Create By JueShou -- 2019-08-12 09:56
 * Project: 动态详情
 */
@Route(path = ARouterConstant.HLDynamicDetailActivity)
public class HLDynamicDetailActivity extends XActivity<HLDynamicDetailPresenter> {
    @Autowired
    String id;
    @BindView(R2.id.title_bar)
    CommonTitleBar mTitleBar;
    @BindView(R2.id.webView)
    WebView mWebView;
    @BindView(R2.id.textView7)
    TextView mTextView7;
    @BindView(R2.id.textView8)
    TextView mTextView8;
    @BindView(R2.id.tv_like)
    TextView mTvLike;
    @BindView(R2.id.ll_like)
    LinearLayout mLlLike;
    @BindView(R2.id.tv_share)
    TextView mTvShare;
    @BindView(R2.id.ll_share)
    LinearLayout mLlShare;
    @BindView(R2.id.linearLayout2)
    LinearLayout mLinearLayout2;
    @BindView(R2.id.textView9)
    TextView mTextView9;
    @BindView(R2.id.textView10)
    TextView mTextView10;
    @BindView(R2.id.recyclerView2)
    RecyclerView mRecyclerView2;
    @BindView(R2.id.iv_collection)
    ImageView mIvCollection;
    @BindView(R2.id.iv_share)
    ImageView mIvShare;
    @BindView(R2.id.linearLayout3)
    LinearLayout mLinearLayout3;
    @BindView(R2.id.imageView4)
    ImageView mImageView4;
    @BindView(R2.id.scrollView)
    NestedScrollView mScrollView;

    private HLDynamicDetailCommentAdapter mCommentAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.hl_activity_dynamic_detail;
    }

    @Override
    public HLDynamicDetailPresenter newP() {
        return new HLDynamicDetailPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTitleBar.setListener((v, action, extra) -> {
            switch (action) {
                case CommonTitleBar.ACTION_LEFT_BUTTON:
                    finish();
                    break;
                case CommonTitleBar.ACTION_RIGHT_BUTTON:
                    RxToast.normal("分享");
                    break;
            }
        });
        mTitleBar.getCenterTextView().setText("链资讯");
        onclick();

        WebSettings webSettings = mWebView.getSettings();
        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);

        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.loadUrl("http://www.imahi.cn/liulan.aspx?id=932975");

        mCommentAdapter = new HLDynamicDetailCommentAdapter(new ArrayList<>());
        mRecyclerView2.setLayoutManager(new LinearLayoutManager(context));
        mRecyclerView2.addItemDecoration(new SuperDividerItemDecoration(new SuperDividerItemDecoration.Builder(context)));
        mRecyclerView2.setNestedScrollingEnabled(false);
        mRecyclerView2.setAdapter(mCommentAdapter);

        getP().getData(id);
    }

    @SuppressLint("CheckResult")
    private void onclick() {
        RxView.clicks(mLlLike).throttleFirst(1, TimeUnit.SECONDS).subscribe(b -> {
            RxToast.normal("喜欢");
        });
        RxView.clicks(mLlShare).throttleFirst(1, TimeUnit.SECONDS).subscribe(b -> {
            RxToast.normal("分享");
        });
        RxView.clicks(mIvCollection).throttleFirst(1, TimeUnit.SECONDS).subscribe(b -> {
            RxToast.normal("收藏");
        });
        RxView.clicks(mIvShare).throttleFirst(1, TimeUnit.SECONDS).subscribe(b -> {
            RxToast.normal("分享");
        });
    }

    public void setNewData(HLDynamicDetailEntity pNewData) {
        mScrollView.scrollTo(0, 0);
        mCommentAdapter.setNewData(pNewData.getCommentEntityList());
    }
}
